home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: Interpreting whitespace w. cin
- Date: 31 Jan 1996 17:18:32 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan31121833@g7240065.bridge.bst.bls.com>
- References: <4emb7p$u0@guitar.hal.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: blair@hal.COM's message of 30 Jan 1996 15:56:41 -0800
-
- In article <4emb7p$u0@guitar.hal.com> blair@hal.COM (Blair Martin) writes:
-
- : I want to write a program that uses new and delete to dynamically
- : allocate exactly enough free memory to hold string input data.
-
- : That is, instead of allocating a fixed array of
- : characters from the stack, receive one character at a time and
- : continually allocate, copy, and release space so that the physical
- : and logical lengths of the buffer area are always the same.
- : When the newline character is encountered, print the string
- : and release the space. Then prompt for the next string. When
- : EOF is entered, terminate the program.
-
-
- : The problem is with a loop like
-
- : while (!(cin >> input).eof())
- : {
- : ...
- : }
-
- : there is no way to know when a newline is encountered since
- : all whitespace is ignored.
-
- : Is there another way to do this?
-
- As I understand this you are wanting to read the whole line of
- unspecified length into a string. Print it out - forget about it and
- continue to the next line.
-
- char ch;
- ostrstream ostr;
- while (cin >> ch)
- {
- ostr << ch;
-
- if (ch == '\n') {
- ostr << ends;
- cout << ostr.str();
- ostr.rbbuf->freeze(0);
- ostr.seekp(0);
- }
- }
-
- The only problem with this is you cannot guarentee the exact length of allocated
- memory for the string (ostr.str()) because it is managed by the ostrstream,
- it does the necessary allocating and reallocating of memory.
- Otherwise it does exactly what you want.
-
- Regards
-
- -A.
- --
- | A.Champion |
-